/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import Facade.TelefonistaFacade;
import VO.TelefonistaVO;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import vista.TelefonistaBean;

/**
 *
 * @author Jorge
 */
public class TelefonistaAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String NEW = "new";
    private final static String EDIT = "edit";
    private final static String ERASE = "erase";
    private final static String BACK = "back";
    private final static String SET = "set";
    private final static String ALL = "all";
    private final static String MENU = "menu";

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward ingresarTel(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(NEW);
    }

    public ActionForward modificarTel(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TelefonistaFacade fcd = new TelefonistaFacade();
        Vector datos = fcd.cargarIdTelefonistas();

        request.setAttribute("telefonistas", datos);
        return mapping.findForward(EDIT);
    }

    public ActionForward turnoTel(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TelefonistaFacade fcd = new TelefonistaFacade();
        Vector datos = fcd.cargarIdTelefonistas();

        request.setAttribute("telefonistas", datos);
        return mapping.findForward(SET);
    }

    public ActionForward consultarTurnos(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TelefonistaFacade fcd = new TelefonistaFacade();
        Vector resultado = fcd.cargarTurnos();

        request.setAttribute("turnos", resultado);

        return mapping.findForward(ALL);
    }

    public ActionForward Registrar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id, fecha;
        id = request.getParameter("cmbTelefonistas");
        fecha = request.getParameter("fecha");
        TelefonistaFacade fcd = new TelefonistaFacade();
        if (fecha.isEmpty()) {
            String aviso = "<span style='color:red;font-weight:bold'>Debe escoger una fecha</span>";
            request.setAttribute("aviso", aviso);
        } else {

            if (fcd.registrarTurno(id, fecha)) {
                String aviso = "<span style='color:green;font-weight:bold'>Registro exitoso</span>";
                request.setAttribute("aviso", aviso);
            }
        }
        Vector datos = fcd.cargarIdTelefonistas();
        request.setAttribute("telefonistas", datos);
        return mapping.findForward(SET);
    }

    public ActionForward eliminarTel(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        TelefonistaFacade fcd = new TelefonistaFacade();
        Vector datos = fcd.cargarIdTelefonistas();



        request.setAttribute("telefonistas", datos);
        return mapping.findForward(ERASE);
    }

    public ActionForward Eliminar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TelefonistaFacade fcd = new TelefonistaFacade();

        String seleccionada = request.getParameter("cmbTelefonistas");

        if (fcd.eliminarTelefonista(seleccionada)) {
            String aviso = "<span style='color:green;font-weight:bold'>Eliminación exitosa</span>";
            request.setAttribute("aviso", aviso);
        } else {

            String aviso = "<span style='color:red;font-weight:bold'>No se ha podido eliminar la telefonista</span>";
            request.setAttribute("aviso", aviso);
        }

        Vector datos = fcd.cargarIdTelefonistas();
        request.setAttribute("telefonistas", datos);


        return mapping.findForward(ERASE);
    }

    public ActionForward Seleccionar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String telefonista = request.getParameter("cmbTelefonistas");
        TelefonistaFacade fcd = new TelefonistaFacade();
        Vector datos = fcd.cargarIdTelefonistas();
        TelefonistaVO datos2 = fcd.cargarTelefonista(telefonista);
        request.setAttribute("telefonistas", datos);
        request.setAttribute("datosTelefonista", datos2);
        return mapping.findForward(EDIT);

    }

    public ActionForward Modificar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TelefonistaFacade fcd = new TelefonistaFacade();
        TelefonistaBean datos = (TelefonistaBean) form;
        TelefonistaVO edit = new TelefonistaVO();


        if (datos.getApellidos().isEmpty() || datos.getDireccion().isEmpty() || datos.getEmail().isEmpty() || datos.getNombres().isEmpty() || datos.getTelefono().isEmpty()) {
            String aviso = "<span style='color:red;font-weight:bold'>Debe Ingresar todos los campos</span>";
            request.setAttribute("aviso", aviso);
            String telefonista = request.getParameter("cmbTelefonistas");

            Vector datos1 = fcd.cargarIdTelefonistas();
            TelefonistaVO datos2 = fcd.cargarTelefonista(telefonista);
            request.setAttribute("telefonistas", datos1);
            request.setAttribute("datosTelefonista", datos2);

        } else {

            edit.setApellidos(datos.getApellidos());
            edit.setDireccion(datos.getDireccion());
            edit.setEmail(datos.getEmail());
            edit.setNombres(datos.getNombres());
            edit.setTelefono(datos.getTelefono());
            edit.setRut(datos.getRut());

            if (fcd.editarTelefonista(edit)) {
                String aviso = "<span style='color:green;font-weight:bold'>Edición exitosa</span>";
                request.setAttribute("aviso", aviso);
            } else {
                String aviso = "<span style='color:red;font-weight:bold'>Error!: inténtelo de nuevo.</span>";
                request.setAttribute("aviso", aviso);
            }

            Vector datos1 = fcd.cargarIdTelefonistas();
            request.setAttribute("telefonistas", datos1);
        }

        return mapping.findForward(EDIT);
    }

    public ActionForward Ingresar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        TelefonistaBean datos = (TelefonistaBean) form;
        TelefonistaVO nueva = new TelefonistaVO();
        TelefonistaFacade fcd = new TelefonistaFacade();
        nueva.setApellidos(datos.getApellidos());
        nueva.setDireccion(datos.getDireccion());
        nueva.setEmail(datos.getEmail());
        nueva.setNombres(datos.getNombres());
        nueva.setRut(datos.getRut());
        nueva.setTelefono(datos.getTelefono());

        if (nueva.getApellidos().isEmpty() || nueva.getDireccion().isEmpty() || nueva.getEmail().isEmpty() || nueva.getEmail().isEmpty() || nueva.getNombres().isEmpty() || nueva.getRut().isEmpty() || nueva.getTelefono().isEmpty()) {
            String aviso = "<span style='color:red;font-weight:bold'>Debe ingresar todos los campos</span>";
            request.setAttribute("aviso", aviso);

        } else {

            if (datos.validaRut(nueva.getRut())) {
                if (fcd.ingresarTelefonista(nueva)) {
                    String aviso = "<span style='color:green;font-weight:bold'>Ingreso exitoso</span>";
                    request.setAttribute("aviso", aviso);
                    return mapping.findForward(NEW);
                }
            } else
            {
                   String aviso2 = "<span style='color:red;font-weight:bold'>Rut Inválido</span>";
            request.setAttribute("avisoRut", aviso2);
            }

        }

        return mapping.findForward(NEW);
    }

    public ActionForward Cancelar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(BACK);
    }

    public ActionForward Volver(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(MENU);
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward volver(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(BACK);
    }
}